var inicio = 0;

$(document).ready(function () {
    
    $(window).resize(iniciar);
    $(window).scroll(iniciar);
    
    var c_menu = $.cookie('menu');
    if(c_menu == '1') {
        $('.tl_aplicacao .lf_menu').hide();
        $('.tl_aplicacao .lf_menu').css('width','0px');
    }
    iniciar();
    
    $('.btn_menu').click( function() {
        if($('.tl_aplicacao .lf_menu').css('display') == 'none'){
            $.cookie('menu', '0');
            $('.tl_aplicacao .lf_menu').show( "slide", function() { 
                $('.tl_aplicacao .lf_menu').css('width','initial');
                iniciar(); 
            });
        }else{
            $.cookie('menu', '1');
            $('.tl_aplicacao .lf_menu').hide( "slide", function() { 
                $('.tl_aplicacao .lf_menu').css('width','0px'); 
                iniciar(); 
            });
        }
    });
    
    $('.swipebox' ).swipebox();
    
    $('.img_topo,.toque').click( function() {
        $('.swipebox:first-child').click();
    });
    
    $('.telefone').mask('(54) 9999-9999');
    
    $(document).on("submit", "#formulario",function(){
        
        $.post(URLBASE+'/'+CONTROL+'/contato', $(this).serialize() ,function(data) {
            if(data.ok){
                document.getElementById("formulario").reset();
                alert('Mensagem enviada com sucesso!');
            } else alert('Ocorreu um erro. Tente novamente mais tarde!');
        }, 'json');
        
        return false;
    });
});     

function iniciar(){ 
    
    var tempo = 2000;
    
    if(inicio == 1){
        menuTime();
        telaTime();
        subtelaTime();
        
    }else{
        setTimeout( function(){
            menuTime();
            telaTime();
            subtelaTime();
            loader();
            inicio = 1;
        }  , tempo);
    }
}

function telaTime(){
    
    var w_li = '';
    var h_li = '';
    var w_tela = $(window).width();
    var h_tela = $(window).height();
    
    var w_cntimg = $('.content_img').width();
    var h_cntimg = $('.content_img').height();
    w_cntimg = parseInt(w_cntimg) / 2;
    h_cntimg = parseInt(h_cntimg) / 2;
    w_cntimg = '-'+  w_cntimg.toString()+'px';
    h_cntimg = '-'+h_cntimg.toString()+'px';

    $('.content_img').css('margin-top', h_cntimg);
    $('.content_img').css('margin-left',w_cntimg);

    var h_topo = $('.topo').height();
    var h_tlcnt =  parseInt(h_tela) - parseInt(h_topo);
    $('.lf_menu,.rg_cnt').css('height', h_tlcnt);

    var w_lf_menu = $('.lf_menu').width();

    var w_rg_cnt = parseInt(w_tela) - parseInt(w_lf_menu); 
    $('.rg_cnt').css('width', w_rg_cnt);    
}


function subtelaTime(){
    
    var w_li = '';
    var h_li = '';
    var w_tela = $(window).width();
    var h_tela = $(window).height();
    var n_ele = $('.cnt_subpag li').size();
    var div = n_ele > 3 ? 2 : 1;

    switch (n_ele) {
        case 1:
            w_li = '100%';
            break;

        case 2:
            w_li = '50%';
            break;

        case 3:
            w_li = '33.33%';
            break;

        case 4:
            w_li = '50%';
            break;

        case 5:
            w_li = '33.33%';
            break;

        case 6:
            w_li = '33.33%';
            break;

        default: w_li = '33.33%';
    }
    
    var h_topo = $('.topo').height();
    
    h_tela =  parseInt(h_tela) - parseInt(h_topo);
    
    h_li = parseInt(h_tela) / div;

    $('.cnt_subpag li').css('width', w_li);
    $('.cnt_subpag li').css('height', h_li);

    var w_cntimg = $('.sub_cnt_img').width();
    var h_cntimg = $('.sub_cnt_img').height();
    
    w_cntimg = (parseInt(w_cntimg) / 2);
    h_cntimg = (parseInt(h_cntimg) / 2) + 20;
    w_cntimg = '-'+  w_cntimg.toString()+'px';
    h_cntimg = '-'+h_cntimg.toString()+'px';

    $('.sub_cnt_img').css('margin-top', h_cntimg);
    //$('.sub_cnt_img').css('margin-left',w_cntimg);
    
}

function menuTime(){
    var w_li = '';
    var h_li = '';
    var w_tela = $(window).width();
    var h_tela = $(window).height();
    var n_ele = $('.lf_menu li').size();
    var h_topo = $('.topo').height();
    
    h_tela =  parseInt(h_tela) - parseInt(h_topo);
    
    h_li = parseInt(h_tela) / n_ele;
    
    $('.lf_menu li').css('height', h_li);
    

}

function loader(){ 
    $('.tl_loader').fadeOut(); 
}